package com.lib.utils;

import android.app.Activity;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class DeviceManger {
    private Context mContext;
    private DevicePolicyManager mDevicePolicyManager;
    private ComponentName mComponentName;

    private static class Inner {
        private static final DeviceManger INSTANCE = new DeviceManger();
    }

    private DeviceManger() {
    }

    public static DeviceManger with(Context context) {
        if (Inner.INSTANCE.mContext == null) {
            Inner.INSTANCE.mContext = context.getApplicationContext();
            //获取设备管理服务
            Inner.INSTANCE.mDevicePolicyManager = (DevicePolicyManager) context.getApplicationContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
            Inner.INSTANCE.mComponentName = new ComponentName(context, DeviceReceiver.class);
        }
        return Inner.INSTANCE;
    }

    // 测试
    public void test(Context context) {
        DevicePolicyManager dm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);

    }

    // 激活设备管理器
    public void enable(Activity activity, boolean enable, boolean showTip) {
        if (enable) {
            //判断是否激活  如果没有就启动激活设备
            if (mDevicePolicyManager.isAdminActive(mComponentName)) {
                if (showTip) {
                    ToastUtils.makeText(mContext, "已激活", Toast.LENGTH_SHORT).show();
                }
                return;
            }
            /* 某些机型这样写才能打开，不能添加 FLAG_ACTIVITY_NEW_TASK */
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            ComponentName componentName = new ComponentName(activity, DeviceReceiver.class);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "开启权限");
            activity.startActivity(intent);
        } else {
            ToastUtils.makeText(mContext, "已关闭", Toast.LENGTH_SHORT).show();
            mDevicePolicyManager.removeActiveAdmin(mComponentName);
        }
    }

    // 设置解锁方式 不需要激活就可以运行
    public void setNewPwd() {
        Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
        mContext.startActivity(intent);
    }

    // 设置解锁方式 需要激活设备管理器（配置策略）
    public void setLockMethod() {
        // PASSWORD_QUALITY_ALPHABETIC
        // 用户输入的密码必须要有字母（或者其他字符）。
        // PASSWORD_QUALITY_ALPHANUMERIC
        // 用户输入的密码必须要有字母和数字。
        // PASSWORD_QUALITY_NUMERIC
        // 用户输入的密码必须要有数字
        // PASSWORD_QUALITY_SOMETHING
        // 由设计人员决定的。
        // PASSWORD_QUALITY_UNSPECIFIED
        // 对密码没有要求。
        if (mDevicePolicyManager.isAdminActive(mComponentName)) {
            Intent intent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
            mDevicePolicyManager.setPasswordQuality(mComponentName, DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
            mContext.startActivity(intent);
        } else {
            ToastUtils.makeText(mContext, "请先激活设备", Toast.LENGTH_SHORT).show();
        }
    }

    //立刻息屏
    public void lockNow() {
        if (mDevicePolicyManager.isAdminActive(mComponentName)) {
            mDevicePolicyManager.lockNow();
        } else {
            ToastUtils.makeText(mContext, "请先激活设备", Toast.LENGTH_SHORT).show();
        }
    }

    // 恢复出厂设置
    public void wipeData() {
        if (mDevicePolicyManager.isAdminActive(mComponentName)) {
            mDevicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
        } else {
            ToastUtils.makeText(mContext, "请先激活设备", Toast.LENGTH_SHORT).show();
        }
    }

    // 设置密码锁
    public void resetPassword(String password) {
        if (mDevicePolicyManager.isAdminActive(mComponentName)) {
            mDevicePolicyManager.resetPassword(password, DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
        } else {
            ToastUtils.makeText(mContext, "请先激活设备", Toast.LENGTH_SHORT).show();
        }
    }

    // 禁用相机
    public void disableCamera(boolean disabled) {
        if (mDevicePolicyManager.isAdminActive(mComponentName)) {
            mDevicePolicyManager.setCameraDisabled(mComponentName, disabled);
        } else {
            ToastUtils.makeText(mContext, "请先激活设备", Toast.LENGTH_SHORT).show();
        }
    }

    public boolean isEnable() {
        return mDevicePolicyManager.isAdminActive(mComponentName);
    }

    /**
     * @author 一叶丶枫凌
     * Created on 2021-01-27 18:18:26
     */
    public static class DeviceReceiver extends DeviceAdminReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
            String action = intent.getAction();
            XLog.i(action);
        }
    }
}